/** 
 * @Title:  UpdateDialog.java 
 * @author:  Abtswiath丶lxy
 * @data:  2016年6月13日 下午6:20:02 <创建时间>
 * 
 * @history：<以下是历史记录>
 *
 * @modifier: <修改人>
 * @modify date: 2016年6月13日 下午6:20:02 <修改时间>
 * @log: <修改内容>
 *
 * @modifier: <修改人>
 * @modify date: 2016年6月13日 下午6:20:02 <修改时间>
 * @log: <修改内容>
 */
package com.sunmi.usercenter.ui.component;

import org.apache.http.Header;

import com.google.gson.Gson;
import com.loopj.android.http.AsyncHttpResponseHandler;
import com.sunmi.usercenter.R;
import com.sunmi.usercenter.bean.Result;
import com.sunmi.usercenter.bean.Shop;
import com.sunmi.usercenter.bean.rpc.ReqVerifyPassword;
import com.sunmi.usercenter.controller.HttpController;
import com.sunmi.usercenter.utils.MD5Utils;

import android.app.Dialog;
import android.content.Context;
import android.text.Selection;
import android.text.Spannable;
import android.text.method.HideReturnsTransformationMethod;
import android.text.method.PasswordTransformationMethod;
import android.view.Gravity;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

/**
 * TODO<请描述这个类是干什么的>
 * 
 * @author Abtswiath丶lxy
 * @versionCode 1 <每次修改提交前+1>
 */
public class ExitDialog extends Dialog {

	private Context mContext;
	private static ExitDialog exitDialog = null;
	private static HttpController mHttpController;
	private static boolean isHidden = true;//是否隐藏输入框文本
	private static ImageView imageView ;
	private static EditText editTextPad ;
	private static RelativeLayout mRelEye;

	public ExitDialog(Context context, int theme) {
		super(context, theme);
		this.mContext = context;
	}

	public ExitDialog(Context context) {
		super(context);
		this.mContext = context;
	}

	public static ExitDialog createDialog(final Context context,final ExitLintener exitLintener,final Shop shop) {
		exitDialog = new ExitDialog(context, R.style.defaultDialogStyle);
		exitDialog.setContentView(R.layout.dialog_exit);
		exitDialog.getWindow().getAttributes().gravity = Gravity.CENTER;
		exitDialog.setCanceledOnTouchOutside(true);
		exitDialog.setCancelable(false);
		mHttpController = HttpController.getInstance(context);
		editTextPad = (EditText) exitDialog.findViewById(R.id.password);
		imageView = (ImageView) exitDialog.findViewById(R.id.clear);
		mRelEye = (RelativeLayout) exitDialog.findViewById(R.id.rel_eye);
		final TextView textView = (TextView) exitDialog.findViewById(R.id.text_password_error);
		LinearLayout linearLayoutCancel = (LinearLayout) exitDialog.findViewById(R.id.line_cancel);
		final LoadingDialog dialog = LoadingDialog.createDialog(context, context.getString(R.string.waitting));
		LinearLayout linearLayoutExitLogin = (LinearLayout) exitDialog.findViewById(R.id.line_login_out);
		imageView.setBackgroundResource(R.drawable.eye_no);
		textView.setVisibility(View.GONE);
		linearLayoutExitLogin.setOnClickListener(new android.view.View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				final String psd = editTextPad.getText().toString();
				if(psd.length()<6){
					textView.setVisibility(View.VISIBLE);
					return;
				}
				ReqVerifyPassword reqVerifyPassword = new ReqVerifyPassword();
				reqVerifyPassword.setPassword(MD5Utils.md5Password(shop.getUserName(), psd));
				reqVerifyPassword.setuId(shop.getuId());
				mHttpController.verifyPassword(reqVerifyPassword, new AsyncHttpResponseHandler() {
					
					@Override
					public void onStart() {
						super.onStart();
						dialog.show();
					}
					
					@Override
					public void onSuccess(int arg0, Header[] arg1, byte[] arg2) {
						String json = new String(arg2);
						Gson gson = new Gson();
						try {
							Result result = gson.fromJson(json, Result.class);
							if(result!=null&&result.code==1){
								Toast.makeText(context, context.getString(R.string.exit_success), 1000).show();
								exitLintener.exitLogin(psd);
								exitLintener.cancel();
							}else{
								textView.setVisibility(View.VISIBLE);
							}
						} catch (Exception e) {
							Toast.makeText(context, context.getString(R.string.req_error), 1000).show();
							
						}
						dialog.dismiss();
					}
					
					@Override
					public void onFailure(int arg0, Header[] arg1, byte[] arg2, Throwable arg3) {
						Toast.makeText(context, context.getString(R.string.req_error), 1000).show();
						dialog.dismiss();
					}
				});
			}
		});
//		imageView.setOnClickListener(new android.view.View.OnClickListener() {
//			
//			@Override
//			public void onClick(View v) {
//				showPsdOrHidePsd();
//			}
//		});
		
		mRelEye.setOnClickListener(new android.view.View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				showPsdOrHidePsd();
			}
		});
		linearLayoutCancel.setOnClickListener(new android.view.View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				exitLintener.cancel();
			}
		});
		return exitDialog;
	}
	
	/**
	 * 是否显示文本框的内容
	 */
	private static void showPsdOrHidePsd(){
		if(isHidden){
			editTextPad.setTransformationMethod(HideReturnsTransformationMethod.getInstance());
			imageView.setBackgroundResource(R.drawable.eye_yes);
		}else{
			editTextPad.setTransformationMethod(PasswordTransformationMethod.getInstance());
			imageView.setBackgroundResource(R.drawable.eye_no);
		}
		isHidden = !isHidden;
		editTextPad.postInvalidate();
		//切换后将EditText光标置于末尾
        CharSequence charSequence = editTextPad.getText();
        if (charSequence instanceof Spannable) {
            Spannable spanText = (Spannable) charSequence;
            Selection.setSelection(spanText, charSequence.length());
        }
	}
	
	public interface ExitLintener{
		public void cancel();
		public void exitLogin(String password);
	}
	
	@Override
	public void dismiss() {
		super.dismiss();
		if(exitDialog!=null){
			exitDialog = null;
		}
		if(mContext!=null){
			mContext = null;
		}
	}

}
